package com.baitiaojun.common.utils.collection;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import com.baitiaojun.common.utils.object.ObjectUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CollectionUtils {

    /**
     * 判断collection集合是否为空
     * @param collection
     * @return
     */
    public static Boolean isEmpty(Collection<?>... collection) {
        return ObjectUtils.isNull(collection) || Arrays.stream(collection).anyMatch(CollectionUtil::isEmpty);
    }

    /**
     * 判断collection集合是否不为空
     * @param collection
     * @return
     */
    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    /**
     * 判断动态参数是否为空
     * @param args
     * @return
     */
    public static Boolean isEmpty(Object... args) {
        return ObjectUtils.isNull(args) || args.length == 0;
    }

    /**
     * 判断动态参数是否不为空
     * @param args
     * @return
     */
    public static Boolean isNotEmpty(Object... args) {
        return !isEmpty(args);
    }

    /**
     * 判断map是否为空
     * @param map
     * @return
     */
    public static Boolean isEmpty(Map<?, ?> map) {
        return ObjectUtils.isNull(map) || map.isEmpty();
    }

    /**
     * 判断map是否不为空
     * @param map
     * @return
     */
    public static Boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    /**
     * 根据function函数对集合转换为list集合
     * @param collection
     * @param function
     * @return
     * @param <T>
     * @param <R>
     */
    public static <T, R> List<R> convert(Collection<T> collection, Function<T, R> function) {
        return collection.stream().map(function).filter(ObjUtil::isNotNull).collect(Collectors.toList());
    }
}
